﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading.Tasks;
using FileSystem.Models;
using FileSystem.Services.IServices;
using FileSystem.Services.Realization;
using FileSystem.Views;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Xamarin.Forms;

namespace FileSystem.ViewModels {
    /// <summary>
    /// 桌面主页ViewModel
    /// </summary>
    public class MainPageViewModel : ViewModelBase {
        // ******** 构造函数
        /// <summary>
        /// 根目录项存储
        /// </summary>
        private IRootDirItemStorage _rootDirItemStorage;

        // /// <summary>
        // /// 目录项存储
        // /// </summary>
        // private IDirItemStorage _dirItemStorage;

        /// <summary>
        /// 内容页激活服务
        /// </summary>
        private IContentPageActivationService _contentPageActivationService;

        /// <summary>
        /// 内容导航服务
        /// </summary>
        private IContentNavigationService _contentNavigationService;

        public MainPageViewModel(IRootDirItemStorage rootDirItemStorage,
            IContentPageActivationService contentPageActivationService,
            IContentNavigationService contentNavigationService) {
            _rootDirItemStorage = rootDirItemStorage;
            _contentPageActivationService = contentPageActivationService;
            _contentNavigationService = contentNavigationService;
        }

        // ******** 绑定属性

        /// <summary>
        /// 桌面图标集合
        /// </summary>
        public ObservableCollection<IconViewModel> DeskIcons { get; } =
            new ObservableCollection<IconViewModel>();

        /// <summary>
        /// 操作系统菜单项集合
        /// </summary>
        public ObservableCollection<OSMenuItemViewModel> OSMenuItems { get; } =
            new ObservableCollection<OSMenuItemViewModel>();

        /// <summary>
        /// 操作系统菜单展示属性
        /// </summary>
        public bool OSMenuShow {
            get => _osMenuShow;
            set => Set(nameof(OSMenuShow), ref _osMenuShow, value);
        }

        /// <summary>
        /// 操作系统菜单展示属性
        /// </summary>
        private bool _osMenuShow;


        // ******** 绑定命令

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        private RelayCommand _pageAppearingCommand;

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ?? (_pageAppearingCommand =
                new RelayCommand(async () =>
                    await PageAppearingCommandFunction()));

        public async Task PageAppearingCommandFunction() {
            if (_isLoaded)
                return;
            _isLoaded = true;
            NavigationPage.SetHasNavigationBar(MainPage, false);
            NavigationPage.SetHasNavigationBar(
                _contentPageActivationService.Activate(
                    ContentNavigationConstants.FileSystemPage), false);
            NavigationPage.SetHasNavigationBar(
                _contentPageActivationService.Activate(
                    ContentNavigationConstants.SettingPage), false);
            NavigationPage.SetHasNavigationBar(
                _contentPageActivationService.Activate(
                    ContentNavigationConstants.UsersPage), false);
            NavigationPage.SetHasNavigationBar(
                _contentPageActivationService.Activate(
                    ContentNavigationConstants.PhysicalDiskBlockPage), false);
            NavigationPage.SetHasNavigationBar(
                _contentPageActivationService.Activate(
                    ContentNavigationConstants.PaperPage), false);

            DeskIcons.Add(new IconViewModel(_contentNavigationService) {
                IconAndFont = new IconAndFont {
                    Icon = "FolderIcon.png", Font = IconConstants.FileSystemFont
                }
            });

            DeskIcons.Add(new IconViewModel(_contentNavigationService) {
                IconAndFont = new IconAndFont {
                    Icon = "PhysicalDiskBlockIcon.png",
                    Font = IconConstants.PhysicalDiskBlockFont
                }
            });

            OSMenuItems.Add(new OSMenuItemViewModel(_contentNavigationService) {
                IconAndFont =
                    new IconAndFont {Icon = "UserIcon.png", Font = "用户"}
            });
            OSMenuItems.Add(new OSMenuItemViewModel(_contentNavigationService) {
                IconAndFont =
                    new IconAndFont {Icon = "SettingIcon.png", Font = "设置"}
            });
        }

        /// <summary>
        /// 点击操作系统菜单命令
        /// </summary>
        private RelayCommand _clickOSMenuCommand;

        /// <summary>
        /// 点击操作系统菜单命令
        /// </summary>
        public RelayCommand ClickOSMenuCommand =>
            _clickOSMenuCommand ?? (_clickOSMenuCommand =
                new RelayCommand(() => ClickOSMenuCommandFunction()));

        public void ClickOSMenuCommandFunction() {
            OSMenuShow = !OSMenuShow;
        }

        /// <summary>
        /// comment
        /// </summary>
        private RelayCommand<IconAndFont> _clickOsMenuItemCommand;

        /// <summary>
        /// comment
        /// </summary>
        public RelayCommand<IconAndFont> ClickOsMenuItemCommand =>
            _clickOsMenuItemCommand ?? (_clickOsMenuItemCommand =
                new RelayCommand<IconAndFont>(iconAndFont =>
                    ClickOsMenuItemCommandFunction(iconAndFont)));

        public void ClickOsMenuItemCommandFunction(IconAndFont iconAndFont) {
            if (iconAndFont.Font == "用户")
                _contentNavigationService.NavigateToAsync(
                    ContentNavigationConstants.UsersPage);
            else if (iconAndFont.Font == "设置")
                _contentNavigationService.NavigateToAsync(
                    ContentNavigationConstants.SettingPage);
        }

        // ******** 公开变量

        // ******** 私有变量
        /// <summary>
        /// NavigationPage
        /// </summary>
        public NavigationPage NavigationPage =>
            _navigationPage ?? (_navigationPage =
                Application.Current.MainPage as NavigationPage);

        /// <summary>
        /// NavigationPage
        /// </summary>
        private NavigationPage _navigationPage;

        /// <summary>
        /// MainPage
        /// </summary>
        public MainPage MainPage =>
            _mainPage ?? (_mainPage = NavigationPage.RootPage as MainPage);

        /// <summary>
        /// MainPage
        /// </summary>
        private MainPage _mainPage;

        /// <summary>
        /// 页面是否已经加载
        /// </summary>
        private volatile bool _isLoaded;
    }
}